#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void routine(void *);
void *fun1(void *);

int main(int argc, char *argv[]) {

  pthread_t tid;
  pthread_create(&tid, NULL, fun1, NULL);

  sleep(5);
  pthread_cancel(tid);

  pthread_exit(NULL);
}

void *fun1(void *argv) {
  pthread_cleanup_push(routine, (void *)"123");
  int i = 0;
  while (i++ < 10) {
    printf("hello world\n");
    sleep(1);
    if(i == 3)
      break;
  }

  pthread_cleanup_pop(1);
  pthread_exit(NULL);
}

void routine(void *data) { printf("This  routine is %s\n", (char *)data); }
